home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8638 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  92 lines

  1. Path: news.rain.org!usenet
  2. From: "Guus Leeuw jr." <guusl@eiffel.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What is wrong with the code please?
  5. Date: Sun, 25 Feb 1996 10:47:59 -0800
  6. Organization: Interactive Software Engineering Inc. http://www.eiffel.com/
  7. Message-ID: <3130AEDF.3FFCCCBE@eiffel.com>
  8. References: <4ghbip$a68@nuke.csu.net>
  9. NNTP-Posting-Host: @outback.eiffel.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
  14.  
  15. Syrous Etezadi Amoli wrote:
  16. > Hi
  17. > Please tell me what is wrong with the following code which implements a
  18. > linked list in BC++ 4.5 with TListImp?
  19. > #include <classlib\listimp.h>
  20. > #include <iostream.h>
  21. > #include <String.h>
  22. > void main()
  23. > {
  24. > class CIndexListElement : public: string{
  25. >  private:
  26. >   string subject;
  27. >   long start;
  28. >   long end;
  29. >  public:
  30. >   DWORD GetStart(void) {return start;};
  31. >   DWORD GetEnd(void) { return end;};
  32. >   string GetSubject(void) {return subject;};
  33. >   void SetStart(DWORD x) {start = x;};
  34. >   void SetEnd(DWORD x) {end = x;};
  35. >   void SetSubject(string s) { subject = s;};
  36. >   private:
  37. > };
  38. > TListImp<CIndexListElement> m_SubjectList;
  39. > CIndexListElement *new_entry=  new CIndexListElement;
  40. > new_entry->SetSubject("1st subject");
  41. > new_entry->SetEnd(1);
  42. > new_entry->SetStart(1);
  43. > m_SubjectList.Add(new_entry);
  44.  
  45. I bet this call to `Add' fails to compile, huh?
  46.  
  47. Let's see... new_entry is a pointer to an object of type CIndexListElement;
  48. m_SubjectList is an object of type TListImp<CIndexListElement>. Hmm, your
  49. template is instantiated with type CIndexListElement and you're trying to put
  50. pointers in it. Either, dereference your pointer, or instantiate your
  51. template to hold CIndexListElement*.
  52.  
  53. > new_entry->SetSubject("2ND subject");
  54. > new_entry->SetEnd(2);
  55. > new_entry->SetStart(2);
  56. > m_SubjectList.Add(new_entry);
  57. > new_entry->SetSubject("3RD subject");
  58. > new_entry->SetEnd(3);
  59. > new_entry->SetStart(3);
  60. > m_SubjectList.Add(new_entry);
  61.  
  62. When you declare m_SubjectList to be a TListImp<CIndexListElement*>, you
  63. gotta be darn sure, that the list copies the objects put in it (i.e. does Add
  64. clone the argument?). If not, you might want to get new objects of type
  65. CIndexListElement for every Add.
  66.  
  67. > TListIteratorImp<cIndexListElement> next(m_SubjectList);
  68. > cout << "\n The stored items are:\n" << next++;
  69. > while (next)
  70. >   cout << ", " << next++;
  71. > cout << endl;
  72.  
  73. Here is one (maybe two) other problem:
  74. a) you don't delete new_entry;
  75. (b) your program doesn't return a result. Plus that main is declared to
  76. return a void type...)
  77. > }
  78.  
  79. Hope this helps,
  80.     Guus
  81.